In [1]:
%matplotlib inline

Simple Loops

01.Numbers from 1 to 100

Problem: Write a program that prints the numbers from 1 to 100, one per line.


In [4]:
for i in range(1,101):
    print(i)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

02. Numbers Ending in 7

Problem: Write a program that prints the numbers in the range [1 ... 1000], ending at 7.


In [6]:
for i in range(1,1001):
    if i % 10 == 7:
        print(i)


7
17
27
37
47
57
67
77
87
97
107
117
127
137
147
157
167
177
187
197
207
217
227
237
247
257
267
277
287
297
307
317
327
337
347
357
367
377
387
397
407
417
427
437
447
457
467
477
487
497
507
517
527
537
547
557
567
577
587
597
607
617
627
637
647
657
667
677
687
697
707
717
727
737
747
757
767
777
787
797
807
817
827
837
847
857
867
877
887
897
907
917
927
937
947
957
967
977
987
997

03.Latin Letters

Problem: Write a program that prints all the letters of the Latin alphabet: a, b, c, ..., z.


In [9]:
for i in range(97,123):
    print(chr(i))


a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

04.Sum Numbers

Problem: Write a program that reads a number of integer numbers entered by the user and sums them.


In [12]:
num_of_loops = int(input())

sum = 0;

for i in range(1, num_of_loops + 1):
    num = int(input())

    sum += num

print(sum)


5
5
5
4
4
4
22

05.Max Number

Problem: Write a program that reads n number of integers (n > 0) entered by the user, the greatest among them. First, the number of ns is entered, and then the n numbers themselves, one per line.


In [14]:
num_of_loops = int(input())

new_num = 0
max_num = 9223372036854775807 * (-1)


for i in range(1 , num_of_loops + 1):
    new_num = int(input())

    if  new_num > max_num:
        max_num = new_num


print(max_num)


2
5
100
100

06.Min Number

Problem: Write a program that reads n number of integers (n > 0) entered by the user, few of them. First, the number of ns is entered, and then the n numbers themselves, one per line.


In [16]:
num_of_loops = int(input())

min_num = 9223372036854775807
new_num  = 0

for i in range(1, num_of_loops + 1):
    new_num = int(input())

    if new_num < min_num:
        min_num = new_num

print(min_num)


5
5
6
7
8
9
5

07.Left and Right Sum

Problem: Write a program that reads 2 * ns of whole integers filed by the user and checks if the sum of the first n numbers (left sum) is equal to the sum of the second n numbers (right amount). In case of equality print "Yes" + the sum; otherwise "No" + difference. The difference is calculated as a positive number (inabsolute value).


In [18]:
import  math

num_of_loops  = int(input())

first_sum = 0
second_sum = 0

difference = 0

for i in range(1, num_of_loops * 2 + 1):

    number = int(input())

    if i <= num_of_loops:
        first_sum += number

    else:
        second_sum += number


difference = math.fabs(first_sum - second_sum)

if difference == 0:
    print(f"Yes, sum = {first_sum}")

else:
    print(f"No, diff = {difference}")


5
5
9
74
2
3
4
5
9
7
5
No, diff = 63.0

08.Odd Even Sum

Problem: Write a program that reads the numbers submitted by the user and checks the sum of the even positions is equal to the sum of the odd numbers. In the case of equality, the "Yes" + amount is printed; otherwise you can print the "No" + difference. The difference is calculated in absolute terms value.


In [19]:
import  math

num_of_loops = int(input())

odd_sum = 0
even_sum = 0
difference = 0

for i in range(1,num_of_loops + 1):

    num = int(input())

    if i % 2 != 0:
        odd_sum += num

    else:
        even_sum += num

difference = math.fabs(odd_sum - even_sum)

if difference == 0:
    print(f"Yes, sum = {odd_sum}")

else:
    print(f"No, diff = {difference}")


5
9
7
97
25
3
No, diff = 77.0

09.Vowels Sum

Problem: Write a program that reads a text (string) entered by the user and calculates and prints the amount of the values of the uppercase letters in the table below:

letter a e i o u
value 1 2 3 4 5

10.Element equal to the sum of the rest

Problem: Write a program that reads n-number integer numbers entered by the user and checks if among there is a number that is equal to the sum of all others. If there is such an item, print "Yes ,Sum = + its value" otherwise the "No", "Diff = + the difference between the largest element and the sum of the rest" (in absolute terms).


In [22]:
import  math

num_of_loops = int(input())

sum = 0
max_num = 0
new_num = 0

for i in range(1 , num_of_loops + 1):

    new_num = int(input())

    if new_num > max_num:
        max_num = new_num

    sum += new_num



if sum / 2 == max_num:
    print(f"Yes, Sum = {max_num}")

else:
    print(f"No, diff = {math.fabs((sum - max_num) - max_num)}")


5
5
9
47
8
2
No, diff = 23.0

11.Even or Odd positions

Problem: Write a program that reads n-number numbers entered by the user and calculates the amount, minimum and the maximum of the even and odd numbers (1 count). When there is no minimum / maximum item, print "No".


In [24]:
import  math
num_of_loops = int(input())

odd_sum = 0
odd_min = math.inf
odd_max = math.inf * (-1)

even_sum = 0
even_min = math.inf
even_max = math.inf * (-1)

new_num = 0

for i in range(1, num_of_loops + 1):

    new_num = float(input())

    if i % 2 != 0:
        odd_sum += new_num

        if new_num < odd_min:
            odd_min = new_num

        if new_num > odd_max:
            odd_max = new_num

    else:
        even_sum += new_num

        if new_num < even_min:
            even_min = new_num

        if  new_num > even_max:
            even_max = new_num

if odd_sum == 0:
    print(f"OddSum = 0")

else:
    print(f"OddSum = {odd_sum}")

if odd_min != math.inf:
    print(f"OddMin = {odd_min},")

else:
    print(f"OddMin = No,")


if odd_max != (math.inf * (-1)):
    print(f"OddMax = {odd_max},")

else:
    print("OddMax = No,")


if even_sum == 0:
    print(f"EvenSum = 0,")

else:
    print(f"EvenSum = {even_sum},")

if even_min != math.inf:
    print(f"EvenMin = {even_min},")

else:
    print("EvenMin = No,")



if even_max != (math.inf * (-1)):
    print(f"EvenMax = {even_max}")


else:
    print(f"EvenMax = No")


5
8
69
7
8
1
OddSum = 16.0
OddMin = 1.0,
OddMax = 8.0,
EvenSum = 77.0,
EvenMin = 8.0,
EvenMax = 69.0

12.The same pairs

Problem: Given are 2 * n-numbered numbers. The first and the second form a pair, the third and the fourth also, etc. Each pair has value - the sum of its numbers. Write a program that checks if everyone pairs have the same value or print the maximum difference between two consecutive pairs. If all pairs have the same value,print: "Yes, value = {Value} + value". Otherwise print "No, maxdiff = {Difference} " + maximum difference.


In [26]:
import  math

num_of_loops = int(input())

num_1 = 0
num_2 = 0

first_pair = 0
second_pair = 0

max_num_of_equal_pairs = 0
max_diff = 0
difference = 0


for i in range(1, num_of_loops + 1):

     num_1 = int(input())
     num_2 = int(input())

     if i % 2 != 0:
          first_pair = num_1 + num_2

     else:
          second_pair = num_1 + num_2

     if first_pair == second_pair:
          max_num_of_equal_pairs += 1

     difference = math.fabs(first_pair - second_pair)

     if difference > max_diff and i > 1:
          max_diff = difference



if (max_num_of_equal_pairs + 1) == num_of_loops:
     print(f"Yes, value = {first_pair}")

else:
     print(f"No, maxdiff = {max_diff}")


5
9
4
32
2
4
7
8
2
5
8
No, maxdiff = 23.0